package st.business.tepth.business.user.busnumber.workholder.adapter;

import android.graphics.Color;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import st.business.tepth.R;
import st.business.tepth.base.OnMultiItemChooseListener;
import st.shadow.library.ui.badge.BadgeView;
import st.shadow.library.ui.recycler.MultipleItemEntity;

/**
 * @author zhangyu
 * @date 2019/2/27
 */
public class ClassificationAdapter extends BaseQuickAdapter<MultipleItemEntity, BaseViewHolder> {
    private int mOldPosition = 0;
    private OnMultiItemChooseListener<Integer, Integer> mListener;
    private boolean showCount;

    public ClassificationAdapter(int layoutResId,
                                 @Nullable List<MultipleItemEntity> data,
                                 OnMultiItemChooseListener<Integer, Integer> listener,
                                 boolean showCount) {
        super(layoutResId, data);
        mListener = listener;
        this.showCount = showCount;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final MultipleItemEntity item) {
        helper.setText(R.id.tv_name, (CharSequence) item.getField("name"));
        BadgeView badgeView = helper.getView(R.id.tv_count);
        int count = item.getField("count");
        int unResolveCount = item.getField("unSolveCount");
        badgeView.setBadgeCount(count - unResolveCount == 0 ? count :
                count - unResolveCount);
        if (showCount) {
            if (count > 0) {
                badgeView.setVisibility(View.VISIBLE);
                if (unResolveCount > 0) {
                    badgeView.setBackground(9, Color.parseColor("#ed2f62"));
                } else {
                    badgeView.setBackground(9, Color.parseColor("#00ff00"));
                }
            } else {
                badgeView.setVisibility(View.INVISIBLE);
            }
            helper.setVisible(R.id.iv_complete, false);
        } else {
            badgeView.setVisibility(View.INVISIBLE);
            int finish = item.getField("finish");
            helper.setVisible(R.id.iv_complete, finish == 0);
        }
        if (item.getField("checked")) {
            helper.setBackgroundColor(R.id.layout_item, Color.parseColor("#ffffff"));
            helper.setVisible(R.id.iv_indicator, true);
        } else {
            helper.setBackgroundColor(R.id.layout_item, Color.parseColor("#f3f6f9"));
            helper.setVisible(R.id.iv_indicator, false);
        }
        helper.getView(R.id.layout_item).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOldPosition != helper.getAdapterPosition()) {
                    helper.setBackgroundColor(R.id.layout_item, Color.parseColor("#ffffff"));
                    helper.setVisible(R.id.iv_indicator, true);
                    MultipleItemEntity old = getItem(mOldPosition);
                    if (old != null) {
                        old.setField("checked", false);
                        notifyItemChanged(mOldPosition);
                    }
                    item.setField("checked", true);
                    mOldPosition = helper.getAdapterPosition();
                    if (mListener != null) {
                        mListener.onChoose(helper.getAdapterPosition(),
                                (Integer) item.getField("id"));
                    }
                }
            }
        });
    }
}
